<?php

namespace App\Models\Banner;

use App\Models\BaseModel;

class Banner extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at',
        'updated_at',
        'position',
        'start_at',
        'end_at',
        'status'
    ];

    protected $table = 'banner';
    protected $dateFormat = 'U';

    /**
     * getBannerByPosition
     * @param $position
     * @param $city_id
     * @return mixed
     */
    public function getBannerByPosition($position, $city_id){
        $time = time();
        $sql = $this->where('status', 1)
            ->where('start_at', '<=', $time)
            ->where('end_at', '>=', $time)
            ->where('position', $position);
        if(!empty($city_id)){
            $sql = $sql->whereIn('city_id', [0, $city_id]);
        }
        $data = $sql->get()
            ->toArray();
        return $this->after($data);
    }
}
